package com.guodu.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.guodu.base.BaseDao;
import com.guodu.dao.FxzjDao;
import com.guodu.entity.CliqueRed;
import com.guodu.entity.Fxzj;
import com.guodu.entity.FxzjStatus;
import com.guodu.entity.Units;
import com.guodu.vo.FxzjQueryVo;

@Repository("fxzjDao")
public class FxzjDaoImpl extends BaseDao implements FxzjDao{

	
	public void save(Fxzj fxzj) {
		super.save(fxzj);
		
	}
	public int findSizeByVo(FxzjQueryVo fxzjQueryVo, Units units) {
		StringBuffer hql = new StringBuffer("select count(*) from Fxzj f where 1=1");
		if(fxzjQueryVo != null){
			if(fxzjQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from UserInfo u where u.userId = f.createPeople and exists (select 1 from Units n where n.unitsId=u.unitsId and ( n.funitsId = "+fxzjQueryVo.getFunitsId()+" or n.unitsId = "+fxzjQueryVo.getFunitsId()+")))");
			}
			if(fxzjQueryVo.getUnitsId()!=null&&fxzjQueryVo.getUnitsId().intValue()!=0 ){
				if(fxzjQueryVo.getUnitsId() == -1L){
					hql.append(" and exists(select 1 from UserInfo u where u.userId = f.createPeople and exists (select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = '1'))");
				}else{
					hql.append(" and exists(select 1 from UserInfo u where u.userId=f.createPeople and u.unitsId="+fxzjQueryVo.getUnitsId()+")");	
				}				
			}
			if(fxzjQueryVo.getMouthStart() != null && !fxzjQueryVo.getMouthStart().trim().equals("")){
				hql.append(" and  TO_DATE(fjMouth,'yyyy-mm') >= TO_DATE('" +fxzjQueryVo.getMouthStart()+"','yyyy-mm')");
			}
			if(fxzjQueryVo.getMouthEnd() != null && !fxzjQueryVo.getMouthEnd().trim().equals("")){
				hql.append(" and TO_DATE(fjMouth,'yyyy-mm') <= TO_DATE('" +fxzjQueryVo.getMouthEnd()+"','yyyy-mm')"); 
			}
			if(fxzjQueryVo.getStatus() != null && fxzjQueryVo.getStatus().length()>0 && !fxzjQueryVo.getStatus().equals("10")){
				hql.append(" and status ="+fxzjQueryVo.getStatus());
			}
			
		}
		if(units==null){
			hql.append(" and ((status=0 and exists(select 1 from UserInfo u where u.userId = f.createPeople and exists (select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = '1'))) or status=1 or status=3 or status=4) ");
		}else if(units.getUnitsLevel()==1L){
			hql.append("and (status=0 or status=1 or status=2 or (status=3 and exists(select 1 from UserInfo u where u.userId = f.createPeople and exists (select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = '1'))) or status=4)");
		}		
	    hql.append("order by createTime desc");	
	    return super.findInt(hql.toString());
	}

	public List<Fxzj> findByVo(FxzjQueryVo fxzjQueryVo, Units units,
			int curPage, int pageSize) {
		StringBuffer hql = new StringBuffer("from Fxzj f where 1=1");
		if(fxzjQueryVo != null){
			if(fxzjQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from UserInfo u where u.userId = f.createPeople and exists (select 1 from Units n where n.unitsId=u.unitsId and ( n.funitsId = "+fxzjQueryVo.getFunitsId()+" or n.unitsId = "+fxzjQueryVo.getFunitsId()+")))");
			}
			if(fxzjQueryVo.getUnitsId()!=null&&fxzjQueryVo.getUnitsId().intValue()!=0 ){
				if(fxzjQueryVo.getUnitsId() == -1L){
					hql.append(" and exists(select 1 from UserInfo u where u.userId = f.createPeople and exists (select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = '1'))");
				}else{
					hql.append(" and exists(select 1 from UserInfo u where u.userId=f.createPeople and u.unitsId="+fxzjQueryVo.getUnitsId()+")");	
				}
				//hql.append(" and exists(select 1 from UserInfo u where u.userId=f.createPeople and u.unitsId="+fxzjQueryVo.getUnitsId()+")");
			}
			if(fxzjQueryVo.getMouthStart() != null && !fxzjQueryVo.getMouthStart().trim().equals("")){
				hql.append(" and  TO_DATE(fjMouth,'yyyy-mm') >= TO_DATE('" +fxzjQueryVo.getMouthStart()+"','yyyy-mm')");
			}
			if(fxzjQueryVo.getMouthEnd() != null && !fxzjQueryVo.getMouthEnd().trim().equals("")){
				hql.append(" and TO_DATE(fjMouth,'yyyy-mm') <= TO_DATE('" +fxzjQueryVo.getMouthEnd()+"','yyyy-mm')"); 
			}
			if(fxzjQueryVo.getStatus() != null && fxzjQueryVo.getStatus().length()>0 && !fxzjQueryVo.getStatus().equals("10")){
				hql.append(" and status ="+fxzjQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and ((status=0 and exists(select 1 from UserInfo u where u.userId = f.createPeople and exists (select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = '1'))) or status=1 or status=3 or status=4) ");
		}else if(units.getUnitsLevel()==1L){
			hql.append("and (status=0 or status=1 or status=2 or (status=3 and exists(select 1 from UserInfo u where u.userId = f.createPeople and exists (select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = 1))) or status=4)");
		}		
	    hql.append(" order by fxzjId desc");
	    return super.findPages(hql.toString(), curPage, pageSize);
	}
	@Override
	public void examinAll(FxzjQueryVo fxzjQueryVo, Units units) {
		StringBuffer hql = new StringBuffer();
		if(units==null){
			hql.append("update from Fxzj f set f.status=3 where f.status=0 ");
		}else if(units.getUnitsLevel()==1L){
			hql.append("update from Fxzj f set f.status=1 where f.status=0 ");
		}
		if(fxzjQueryVo != null){
			if(fxzjQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from UserInfo u where u.userId = f.createPeople and exists (select 1 from Units n where n.unitsId=u.unitsId and ( n.funitsId = "+fxzjQueryVo.getFunitsId()+" or n.unitsId = "+fxzjQueryVo.getFunitsId()+")))");
			}
			if(fxzjQueryVo.getUnitsId()!=null&&fxzjQueryVo.getUnitsId().intValue()!=0){
				if(fxzjQueryVo.getUnitsId() == -1L){
					hql.append(" and exists(select 1 from UserInfo u where u.userId = f.createPeople and exists (select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = '1'))");
				}else{
					hql.append(" and exists(select 1 from UserInfo u where u.userId=f.createPeople and u.unitsId="+fxzjQueryVo.getUnitsId()+")");	
				}
			//	hql.append(" and exists(select 1 from UserInfo u where u.userId=f.createPeople and u.unitsId="+fxzjQueryVo.getUnitsId()+")");
			}
			if(fxzjQueryVo.getMouthStart() != null && !fxzjQueryVo.getMouthStart().trim().equals("")){
				hql.append(" and  TO_DATE(fjMouth,'yyyy-mm') >= TO_DATE('" +fxzjQueryVo.getMouthStart()+"','yyyy-mm')");
			}
			if(fxzjQueryVo.getMouthEnd() != null && !fxzjQueryVo.getMouthEnd().trim().equals("")){
				hql.append(" and TO_DATE(fjMouth,'yyyy-mm') <= TO_DATE('" +fxzjQueryVo.getMouthEnd()+"','yyyy-mm')"); 
			}
			if(fxzjQueryVo.getStatus() != null && fxzjQueryVo.getStatus().length()>0 && !fxzjQueryVo.getStatus().equals("10")){
				hql.append(" and status ="+fxzjQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and ((status=0 and exists(select 1 from UserInfo u where u.userId = f.createPeople and exists (select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = '1'))) or status=1 or status=3 or status=4) ");
		}else if(units.getUnitsLevel()==1L){
			hql.append("and (status=0 or status=1 or status=2 or (status=3 and exists(select 1 from UserInfo u where u.userId = f.createPeople and exists (select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = 1))) or status=4)");
		}		
		 super.executeUpdate(hql.toString());
	}
	
	public Fxzj findById(String id) {
		
		
		return (Fxzj)super.findById(Fxzj.class,id);
	}
	
	public void update(Fxzj fxzj) {
		super.update(fxzj);
		
	}
	
	public void deleteById(String id) {
		
		 super.delete(Fxzj.class, id);
	}
	public void examine(String ids, Long status) {
		StringBuffer hql = new StringBuffer("update Fxzj set status =");
		hql.append(status);
		hql.append(" where fxzjId in (");
		hql.append(ids);
		hql.append(")");
		super.executeUpdate(hql.toString());
	}
	
	public void changeStatus(List<FxzjStatus> fxzjStatusList) {
		super.saveAll(fxzjStatusList);
		
	}
	
	public List<FxzjStatus> findStatus(String id) {
		
		return super.findByProperty(FxzjStatus.class,"fxzjId",id);
	}
	

}
